<template>
  <div>
    <RePage>
      <MyFormCreate v-model="searchForm" :rule="searchFormRules" :key="key">
        <template #field-createdAt="scope">
          <el-form-item>
            <el-date-picker type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
              value-format="YYYY-MM-DD" placeholder="请选择创建时间" style="width: 100%" v-model="searchForm.createdAt"
              @change="dateChange" />
          </el-form-item>
        </template>
        <el-button type="primary" @click="DebounceGetTableList">搜索</el-button>
        <sj-button custom-type="sinfo" @click="DebounceResetting">重置</sj-button>
      </MyFormCreate>
      <div>
        <el-button v-auth="'business.control-record.delete'" type="danger" :disabled="!selectionData.length" @click="deletes()">批量删除</el-button>
      </div>
      <pure-table class="mt-5" :data="tableData.data" :columns="initPureTableConfig(roleTableConfig)" showOverflowTooltip border
        @selection-change="selectionChange" :isShowPag="false">
        <template #operatePlatform="{ row }">
          {{ isNullOrEmpty( CONTROLTYPETEXT[row.operatePlatform]?.label ) }}
        </template>
        <template #operateResult="{ row }">
          <el-tag :type="row.operateResult == 1? 'success' : 'danger'">
            {{ isNullOrEmpty( CONTROLRESULTTEXT[row.operateResult]?.label ) }}
        </el-tag>
        </template>
      </pure-table>
      <div class="mt-5">
        <el-pagination v-model:current-page="searchForm.page" v-model:page-size="searchForm.limit" background
          :layout="PAGE_LAYOUT" :total="tableData.total" @size-change="getTableList" @current-change="getTableList" />
      </div>
    </RePage>
  </div>
</template>
<script lang="ts" setup>
import {isNullOrEmpty,initPureTableConfig} from '@/utils/functions'
import {  CONTROLTYPETEXT, CONTROLTYPE,CONTROLRESULTTEXT,CONTROLRESULT } from "@/constant/alarm-mangenment";
import MyFormCreate from "@/components/MyFormCreate/index.vue";
import RePage from "@/components/RePage/index.vue";
import ReTable from "@/components/ReTable/index.vue";
import { reactive, ref, h } from "vue";
import { PAGE_LAYOUT } from "@/constant";
import { Plus, Delete } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  getList,
  deleteInfo
} from "@/api/operate/device-management/control-records/index";
import { titles, defaultSearchForm, prompt, defaultInfoForm } from "./index";
import { addDialog } from "@/components/ReDialog";
import {
  cloneDeep,
  isAllEmpty,
  deviceDetection,
  debounce
} from "@pureadmin/utils";
import { message } from "@/utils/message";
import { customDebounce, customPromiseDebounce } from "@/utils/functions";

const searchForm = ref(defaultSearchForm());
const key = ref(0);
const showInfo = ref();
const formRef = ref();
const tableData = reactive({
  data: [],
  total: 0
});

const searchFormRules = [
  {
    type: "input",
    field: "deviceSN",
    title: "设备序列号",
    value: null,
    props: {
      placeholder: "请输入设备序列号",
      clearable: true
    }
  },
  {
    type: "select",
    field: "operatePlatform",
    title: "控制方式",
    value: null,
    options: CONTROLTYPE,
    props: {
      placeholder: "请选择控制方式",
      clearable: true
    }
  },
  {
    type: "select",
    field: "result",
    title: "操作状态",
    value: null,
    options: CONTROLRESULT,
    props: {
      placeholder: "请选择操作状态",
      clearable: true
    }
  },
  {
    type: "fieldComponent",
    field: "createdAt",
    title: titles.createdAt,
    value: null,
    props: {
      placeholder: prompt.createdAt,
      clearable: true
    }
  },
  { type: "number", field: "page", title: "page", hidden: true },
  { type: "number", field: "limit", title: "limit", hidden: true }
];
const selectionData = ref([]);
const roleTableConfig = [
  { type: "selection", align: "left" },
  { prop: "id", label: titles.id, width: 100 },
  { prop: "unityName", label: titles.unityName, minWidth: 160 },
  { prop: "projectName", label: titles.projectName, minWidth: 160 },
  { prop: "productName", label: titles.deviceId, minWidth: 120 },
  { prop: "deviceSN", label: "设备序列号", minWidth: 100 },
  { prop: "operateContent", label: titles.deviceName, minWidth: 120 },
  { slot: "operatePlatform", label: titles.noticeType, minWidth: 100 },
  { slot: "operateResult", label: titles.content, minWidth: 90 },
  { prop: "operateParam", label: titles.operateParam, minWidth: 120 },
  { prop: "operateResultContent", label: titles.operateResultContent, minWidth: 120 },
  { prop: "operator", label: titles.status, minWidth: 90 },
  { prop: "createdAt", label: titles.createdAt, minWidth: 160 }
  // { slot: 'action', label: titles.action, width: 150, fixed: 'right' },
];
onMounted(() => {
  getTableList();
});
const getTableList = () => {
  getList(searchForm.value).then(res => {
    if (res.code === 200) {
      tableData.data = res.result.data;
      tableData.total = res.result.total;
    }
  });
};
// 重置搜索栏
const resetting = () => {
  //点击重置按键
  Object.assign(searchForm.value, defaultSearchForm());
  key.value++;
  getTableList();
};
const dateChange = e => {
  if (e) {
    searchForm.value.startTime = e[0];
    searchForm.value.endTime = e[1];
  } else {
    searchForm.value.startTime = '';
    searchForm.value.endTime = '';
  }
  getTableList()
};
// 新增
const add = row => {
  showInfo.value = {};
  openDialog();
};
// 编辑
const edit = row => {
  showInfo.value = {};
  openDialog("编辑", row);
};
// 删除
const deletes = (row?) => {
  ElMessageBox.confirm(`确认删除${row ? "当前" : "选中的"}数据?`, "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteInfo({
      ids: row ? [row.id] : selectionData.value.map(item => item.id)
    }).then(res => {
      if (res.code == 200) {
        ElMessage({
          type: "success",
          message: "删除成功"
        });
        getTableList();
      }
    });
  });
};
// 开启弹窗
const selectionChange = data => {
  selectionData.value = data;
};
// 获取列表
const DebounceGetTableList = debounce(getTableList, 1000, true);
// 重置
const DebounceResetting = debounce(resetting, 1000, true);
</script>
<style lang="scss" scoped></style>
